package com.mmy.controller.ship;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.ship.TShipPlan;
import com.mmy.domain.ship.VShipBase;
import com.mmy.domain.ship.VShipPlan;
import com.mmy.domain.ship.VShipPort;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.ship.impl.TShipBaseServiceImpl;
import com.mmy.service.ship.impl.TShipPlanServiceImpl;
import com.mmy.service.ship.impl.TShipPortServiceImpl;

/**
 * 类的介绍：航班时刻
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */

@Controller
public class ShipPlanController {

	@Autowired
	TShipPlanServiceImpl tShipPlanServiceImpl;
	@Autowired
	TShipBaseServiceImpl tShipBaseServiceImpl;
	@Autowired
	TShipPortServiceImpl tShipPortServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/shipplan")
	public String ToShipPlan(Model model) {
		try {

			List<VShipPlan> lstShipPlan = tShipPlanServiceImpl.findAll();
			model.addAttribute("shipplanlist", lstShipPlan);

			return "webbackground/ship/shipplan";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createshipplan")
	public String ToCreateShipPlan(Model model) {

		List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
		model.addAttribute("shipbaselist", lstShipBase);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		List<VShipPort> lstShipPort = tShipPortServiceImpl.findAll();
		model.addAttribute("slist", lstShipPort);

		return "webbackground/ship/createshipplan";
	}

	@RequestMapping(value = "/createoutshipplan")
	public String ToCreateOutShipPlan(Model model) {

		List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
		model.addAttribute("shipbaselist", lstShipBase);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		List<VShipPort> lstShipPort = tShipPortServiceImpl.findAll();
		model.addAttribute("slist", lstShipPort);

		return "webbackground/ship/createoutshipplan";
	}

	@RequestMapping(value = "/shipplaninfo")
	public String ToShipPlanInfo(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		VShipPlan vShipPlan = tShipPlanServiceImpl.findByID(iPlanID);
		model.addAttribute("vShipPlan", vShipPlan);

		return "webbackground/ship/shipplaninfo";
	}

	/**
	 * 函数功能说明： 新建游轮航程
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/subcreateshipplan")
	public String CreateShipPlan(TShipPlan tShipPlan, String startTime, String endTime, HttpSession session)
			throws Exception {

		tShipPlan.setStartDate(common.formatDate(startTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
		tShipPlan.setEndDate(common.formatDate(endTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));

		// 获取时长
		tShipPlan.setLong(common.getHourBetweenTwoDay(startTime, endTime));

		// 获取最大值
		tShipPlan.setPlanID(common.createPlanCode(UtilConstants.TableName.T_SHIP_PLAN));

		try {

			tShipPlan.setCreateDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tShipPlan.setCreator(tSysUser.getUserName());
			tShipPlanServiceImpl.save(tShipPlan.getPlanID());
			tShipPlanServiceImpl.save(tShipPlan);
			return "redirect:shipplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除游轮航程
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delshipplan")
	@ResponseBody
	public String DelShipPlan(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstPlanID = new ArrayList<>();

		for (String str : checklist) {
			lstPlanID.add(Integer.parseInt(str));
		}

		tShipPlanServiceImpl.delShipPlan(lstPlanID);

		return "webbackground/ship/shipplan";
	}

	/**
	 * 函数功能说明： 修改游轮航程
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateshipplan")
	public String EnterUpdateShipPlan(HttpServletRequest request, Model model) {

		int iPlanID = Integer.parseInt(request.getParameter("iPlanID"));

		TShipPlan tShipPlan = tShipPlanServiceImpl.findByPlanID(iPlanID);
		model.addAttribute("tShipPlan", tShipPlan);
		List<VShipBase> lstShipBase = tShipBaseServiceImpl.findAll();
		model.addAttribute("shipbaselist", lstShipBase);
		List<TConstZone> lstConstZone = constZoneServiceImpl.findAllCountriess();
		model.addAttribute("llist", lstConstZone);
		List<VShipPort> lstShipPort = tShipPortServiceImpl.findAll();
		model.addAttribute("slist", lstShipPort);

		return "webbackground/ship/updateshipplan";

	}

	@RequestMapping(value = "/updateshipplan")
	public String UpdateShipPlan(TShipPlan tShipPlan, HttpSession session) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TShipPlan dtShipPlan = tShipPlanServiceImpl.findByPlanID(tShipPlan.getPlanID());
			dtShipPlan.setStandard(tShipPlan.getStandard());
			dtShipPlan.setShipID(tShipPlan.getShipID());
			dtShipPlan.setZoneOutID(tShipPlan.getZoneOutID());
			dtShipPlan.setPortOutID(tShipPlan.getPortOutID());
			dtShipPlan.setZoneInID(tShipPlan.getZoneInID());
			dtShipPlan.setPortInID(tShipPlan.getPortInID());
			dtShipPlan.setFloor(tShipPlan.getFloor());
			dtShipPlan.setPeer(tShipPlan.getPeer());
			dtShipPlan.setPrice(tShipPlan.getPrice());
			dtShipPlan.setNumber(tShipPlan.getNumber());
			dtShipPlan.setMemo(tShipPlan.getMemo());
			dtShipPlan.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtShipPlan.setReviser(tSysUser.getUserName());
			tShipPlanServiceImpl.save(dtShipPlan);
			return "redirect:shipplan";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询航班时刻
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchshipplan")
	public String FindShipPlan(HttpServletRequest request, Model model) {

		String strShipName = request.getParameter("ShipName");
		if (strShipName == "") {
			List<VShipPlan> lstShipPlan = tShipPlanServiceImpl.findAll();
			model.addAttribute("shipplanlist", lstShipPlan);
			request.setAttribute("strShipName", strShipName);
			return "webbackground/ship/shipplan";
		} else {
			List<VShipPlan> lstShipPlan = tShipPlanServiceImpl.findByName(strShipName);
			model.addAttribute("shipplanlist", lstShipPlan);
			request.setAttribute("strShipName", strShipName);
			return "webbackground/ship/shipplan";
		}

	}

}
